//
//  XSTabBarController.m
//  AudioVideo
//
//  Created by xueshan1 on 2019/8/29.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "XSTabBarController.h"
#import "XSNavigationController.h"

@interface XSTabBarController ()<UITabBarControllerDelegate>

@end

@implementation XSTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSArray *array = @[
                       @{@"clsName": @"XSHomeViewController", @"title": @"首页",@"imageName":@"首页"},
                       @{@"clsName": @"XSMineViewController", @"title": @"我的",@"imageName":@"全部"},
 
                       ];
    
    for (NSDictionary *dic in array) {
        [self setChildControllerWithDictionary:dic];
    }
    
    self.delegate = self;
    
    NSNumber *index = [[NSUserDefaults standardUserDefaults]objectForKey:@"lastSelected"];
    self.selectedIndex = [index unsignedIntegerValue];
}

- (void)setChildControllerWithDictionary:(NSDictionary *)dic{
    
    Class  clazz = NSClassFromString(dic[@"clsName"]);
    UIViewController *vc = [[clazz alloc] initWithNibName:dic[@"clsName"] bundle:nil];
 
    vc.title = dic[@"title"];
    NSString *name1 = [NSString stringWithFormat:@"%@-未点击",dic[@"imageName"]];
    NSString *name2 = [NSString stringWithFormat:@"%@-点击",dic[@"imageName"]];
    
    vc.tabBarItem.image = [[UIImage imageNamed:name1] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:name2] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]} forState:UIControlStateNormal];
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:Theme_Color} forState:UIControlStateHighlighted];
    
    XSNavigationController *nav = [[XSNavigationController alloc]initWithRootViewController:vc];
    [self addChildViewController:nav];
    
}


- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    NSInteger index = tabBarController.selectedIndex;
    [[NSUserDefaults standardUserDefaults]setObject:@(index) forKey:@"lastSelected"];
    
    
}





@end
